package com.sgb.kjwl.view.ui.fragment.machine;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.link.utils.PickCateType;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentAskLeaseBiddingBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.pick.PickBidingAdapter;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.PickHallBidingPopWindow;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.pick.PickChooseLeftDialog2;
import com.sgb.kjwl.viewmodel.machine.AskLeaseBiddingFragmentModel;
import com.sgb.zhaocai.utils.PickCopyUtils;
import com.swgk.core.util.PickSelectTextEntity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 竞价集采
 */
public class AskLeaseBiddingFragment extends AppLazyFragment {

    private FragmentAskLeaseBiddingBinding binding;

    private String classNo = "";
    private String typeNo = "";
    private String sortType = "1";
    private String provinceNo = "";
    private String cityNo = "";
    private String provinceName = "";
    private String cityName = "";
    private String areaId = "";
    //    private LeaseAskMapPopWindow classPopWindow;
//    private LeaseAskMapPopWindow typePopWindow;
    private DictWindow5 sortPopWindow;
    private ThreeLevelAreaWindow mAddressPop;

    private PickBidingAdapter pickingAdapter;
    private String categoryType = "";
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;//省市区
    private List<AskLeaseItemEntity<List<AskLeaseItemEntity>>> allDatas;
    private List<PickSelectEntity> classDatas;
    private List<PickSelectEntity> typeDatas;
    private List<PickSelectEntity> sortData;//综合排序
    private List<PickSelectEntity> stateData;//核算金额
    private String price = "";
    private String timeNo = "";
    private PickHallBidingPopWindow pickMoneyPopWindow;
    private PickHallBidingPopWindow pickSortPopWindow;
    private PickChooseLeftDialog2 mLeftDialog2;
    private boolean isFilter = true;

    @Inject
    AskLeaseBiddingFragmentModel model;

    public static AskLeaseBiddingFragment newInstance(String type, ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>> list, boolean isFilter) {
        AskLeaseBiddingFragment fragment = new AskLeaseBiddingFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        bundle.putSerializable("data", list);
        bundle.putBoolean("isFilter", isFilter);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder().appComponent(appComponent).machineFragModule(new MachineFragModule(this)).build().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_ask_lease_bidding, container, false);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        if (getArguments() != null) {
            categoryType = getArguments().getString("type");
            isFilter = getArguments().getBoolean("isFilter", true);
        }

        if (!TextUtils.isEmpty(categoryType)) {
            allDatas = (ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>>) getArguments().getSerializable("data");
        } else {
            allDatas = new ArrayList<>();
            binding.tabLayout.setVisibility(View.GONE);
            binding.typeLayout.setVisibility(View.GONE);
        }
        if (!isFilter) {
            binding.roomLayout.setVisibility(View.GONE);
            binding.tabType.setVisibility(View.GONE);
        }
        initData();
    }

    @Override
    protected void initView() {
        initChooseDialog();
        initTabType();
        binding.classifyLayout.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        binding.areaLayout.setOnClickListener(this);
        binding.sortLayout.setOnClickListener(this);
        binding.stateLayout.setOnClickListener(this);
        binding.filterLayout.setOnClickListener(this);

        if (pickingAdapter == null) {
            pickingAdapter = new PickBidingAdapter(getActivity(), null);
        }
        pickingAdapter.setType("");//设置改属性是为了取消多余的标签名称
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide2));
        binding.recyclerView.addItemDecoration(divider);

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setAdapter(pickingAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        pickingAdapter.onIChildClick(new PickBidingAdapter.IChildClick() {
            @Override
            public void onClick(BiddingEntity tenderListEntity) {
                if (!ClickUtils.isDoubleClick()) {
                    model.getUrlSetting(tenderListEntity.getId());
                }
            }
        });

//        classPopWindow = new LeaseAskMapPopWindow(getActivity());
//        classPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
//            @Override
//            public void result(Object key1, Object key2, Object key3) {
//                updateChooseTextStatus(binding.classifyLayout, binding.tvClassify, 2, ((PickSelectEntity) key1).getPickerViewText());
//                classNo = ((PickSelectEntity) key1).getNo();
//                if (TextUtils.isEmpty(classNo)) {
//                    typeDatas.clear();
//                    PickSelectEntity entity = new PickSelectEntity();
//                    entity.setNo("");
//                    entity.setProjectscale("不限");
//                    typeDatas.add(0, entity);
//                    typePopWindow.goodsTypeAdapter.setDefItem(0);
//                } else {
//                    int classIndex = (int) key2;
//                    typeDatas.clear();
//                    if ("2".equals(categoryType)) {
//                        typeDatas.addAll(getCarByTypeData());
//                    } else {
//                        for (AskLeaseItemEntity<List<AskLeaseItemEntity>> ll : allDatas.get(classIndex - 1).getChildren()) {
//                            PickSelectEntity child = new PickSelectEntity();
//                            child.setNo(ll.getNo());
//                            child.setProjectscale(ll.getName());
//                            typeDatas.add(child);
//                        }
//                    }
//
//                    PickSelectEntity entity = new PickSelectEntity();
//                    entity.setNo("");
//                    entity.setProjectscale("不限");
//                    typeDatas.add(0, entity);
//                    typePopWindow.goodsTypeAdapter.setDefItem(0);
//                }
//
//                typeNo = "";//选择分类时,将原先的子类型清空
//                initFilterData(false);
//            }
//        });
//        classPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                if ("分类".equals(binding.tvClassify.getText()) || "车型".equals(binding.tvClassify.getText())) {
//                    updateChooseTextStatus(binding.classifyLayout, binding.tvClassify, 0, null);
//                } else {
//                    updateChooseTextStatus(binding.classifyLayout, binding.tvClassify, 2, null);
//                }
//            }
//        });
//
//        typePopWindow = new LeaseAskMapPopWindow(getActivity());
//        typePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
//            @Override
//            public void result(Object key1, Object key2, Object key3) {
//                updateChooseTextStatus(binding.typeLayout, binding.tvType, 2, ((PickSelectEntity) key1).getPickerViewText());
//                typeNo = ((PickSelectEntity) key1).getNo();
//
//                initFilterData(false);
//            }
//        });
//        typePopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                if ("类型".equals(binding.tvType.getText()) || "品牌".equals(binding.tvType.getText())) {
//                    updateChooseTextStatus(binding.typeLayout, binding.tvType, 0, null);
//                } else {
//                    updateChooseTextStatus(binding.typeLayout, binding.tvType, 2, null);
//                }
//            }
//        });

        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(binding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setProvinceSingle();
        mAddressPop.setCitySingle();
        mAddressPop.setAreaSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnResultClick((key1, key2, key3) -> {
            try {
                List<BaseAreaEntity> listKey1 = (List<BaseAreaEntity>) key1;
                List<BaseAreaEntity> listKey2 = (List<BaseAreaEntity>) key2;
                List<BaseAreaEntity> listKey3 = (List<BaseAreaEntity>) key3;
                areaId = "";
                if (listKey3 != null && listKey3.size() > 0) {
                    areaId = listKey3.get(0).getNo();
                }
                if (listKey2 != null && listKey2.size() > 0) {
                    cityNo = listKey2.get(0).getNo();
                    cityName = listKey2.get(0).getName();
                } else {
                    cityNo = "";
                }
                provinceNo = listKey1.get(listKey1.size() - 1).getNo();
                provinceName = listKey1.get(listKey1.size() - 1).getName();
            } catch (Exception exception) {
            }


            initFilterData(false);
            if (!TextUtils.isEmpty(cityName) && "不限".equals(cityName)) {
                updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, cityName);
            } else {
                updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, provinceName);
            }

        });
        mAddressPop.setOnDismissListener(() -> {
            updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? 0 : 2, null);
        });

//        sortPopWindow = new DictWindow5(getActivity());
//        sortPopWindow.setOnCallBack(new DictWindow5.OnCallBack() {
//            @Override
//            public void result(DictEntity3 dictEntity) {
//                updateChooseTextStatus(binding.sortLayout, binding.tvSort, 2, dictEntity.getName());
//                sortType = dictEntity.getId();
//                initFilterData(false);
//            }
//        });
//        sortPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                updateChooseTextStatus(binding.sortLayout, binding.tvSort, "排序".equals(binding.tvSort.getText()) ? 0 : 2, null);
//            }
//        });

        //排序方式
        pickSortPopWindow = new PickHallBidingPopWindow(getActivity());
        pickSortPopWindow.setBgView(binding.grayLayout);
        pickSortPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                String name = ((PickSelectEntity) key1).getProjectscale();
                sortType = ((PickSelectEntity) key1).getNo();
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, 2, name);
                initFilterData(false);
            }
        });
        pickSortPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, "综合排序".equals(binding.tvSort.getText()) ? 0 : 2, null);
            }
        });


        // 金额
        pickMoneyPopWindow = new PickHallBidingPopWindow(getActivity());
        pickMoneyPopWindow.setBgView(binding.grayLayout);
        pickMoneyPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                String name = ((PickSelectEntity) key1).getProjectscale();
                price = ((PickSelectEntity) key1).getNo();
                updateChooseTextStatus(binding.stateLayout, binding.tvState, 2, name);
                initFilterData(false);

                mFilters.put(PickCateType.TYPE_MONEY, (int) key3);
            }
        });
        pickMoneyPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                updateChooseTextStatus(binding.stateLayout, binding.tvState, "核算金额".equals(binding.tvState.getText()) ? 0 : 2, null);
            }
        });


    }

    private void initTabType() {
        //机械列表分类标题
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateChooseTextStatus(binding.typeLayout, binding.tvType, 0, "类型");
                typeNo = "";
                classNo = classDatas.get(tab.getPosition()).getNo();
                if (TextUtils.isEmpty(classNo)) {
                    typeDatas.clear();
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("不限");
                    typeDatas.add(0, entity);
//                    typePopWindow.goodsTypeAdapter.setDefItem(0);
                } else {
                    int classIndex = tab.getPosition();
                    typeDatas.clear();
                    if ("2".equals(categoryType)) {
                        typeDatas.addAll(getCarByTypeData());
                    } else {
                        for (AskLeaseItemEntity ll : allDatas.get(classIndex - 1).getChildren()) {
                            PickSelectEntity child = new PickSelectEntity();
                            child.setNo(ll.getNo());
                            child.setProjectscale(ll.getName());
                            typeDatas.add(child);
                        }
                    }

                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("不限");
                    typeDatas.add(0, entity);
//                    typePopWindow.goodsTypeAdapter.setDefItem(0);
                }

                initFilterData(false);//TODO
                showTypeTab();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        binding.tabType.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tabType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                checkDismiss(0);
                typeNo = typeDatas.get(tab.getPosition()).getNo();
                initFilterData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initData() {
        if ("2".equals(categoryType)) {
            binding.tvClassify.setText("车型");
            binding.tvType.setText("品牌");
        }
        provinceNo = model.getAreaCodeModel();//默认省参数
        classDatas = new ArrayList<>();
        typeDatas = new ArrayList<>();
        model.getPriceData();
        model.getBidSortSelect();
        model.getPickTimes();
        showClassPop();
        initFilterData(false);
    }

    //因接口返回Json格式不规范,需要在此特别处理一级数据车型
    private void onResetCarByClassData() {
        classDatas.clear();
        for (AskLeaseItemEntity itemEntity : allDatas.get(0).getChildren()) {
            PickSelectEntity selectEntity = new PickSelectEntity();
            selectEntity.setNo(itemEntity.getNo());
            selectEntity.setProjectscale(itemEntity.getName());
            classDatas.add(selectEntity);
        }
    }

    //因接口返回Json格式不规范,转换二级数据品牌
    private List<PickSelectEntity> getCarByTypeData() {
        List<PickSelectEntity> tempData = new ArrayList<>();
        for (AskLeaseItemEntity itemEntity : allDatas.get(1).getChildren()) {
            PickSelectEntity selectEntity = new PickSelectEntity();
            selectEntity.setNo(itemEntity.getNo());
            selectEntity.setProjectscale(itemEntity.getName());
            tempData.add(selectEntity);
        }
        return tempData;
    }


    private void initFilterData(boolean isLoadMore) {
        String goodsType = "";
        String type = "";
//        if (!TextUtils.isEmpty(categoryType)) {
////            goodsType = Integer.parseInt(categoryType) + 1 + "";
//        }
        if ("0".equals(categoryType)) {//机械
            type = "1";
        } else if ("1".equals(categoryType)) {//设备
            type = "5";
        }

        if (!TextUtils.isEmpty(classNo)) {
            goodsType = classNo;
        }
        if (!TextUtils.isEmpty(typeNo)) {
            goodsType = typeNo;
        }
        model.getBiddingList(type, provinceNo, cityNo, areaId, goodsType, price, sortType, timeNo, isLoadMore);
    }


    public void showList(List<BiddingEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (null != list && list.size() > 0) {
                pickingAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                pickingAdapter.setDatas(list);
                binding.recyclerView.setNoMore(false);
            } else {
                pickingAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * @param v
     */
    @Override
    public void onClick(View v) {
        checkDismiss(v.getId());
        switch (v.getId()) {
//            case R.id.classify_layout:
//                if (null != classDatas && classDatas.size() > 0) {
//                    if ("分类".equals(binding.tvClassify.getText()) || "车型".equals(binding.tvClassify.getText())) {
//                        updateChooseTextStatus(binding.classifyLayout, binding.tvClassify, -1, null);
//                    } else {
//                        updateChooseTextStatus(binding.classifyLayout, binding.tvClassify, 1, null);
//                    }
//                    classPopWindow.showPopwindow(binding.classifyLayout);
//                }
//                break;
//            case R.id.type_layout:
//                if (null != typeDatas && typeDatas.size() > 0) {
//                    showTypePop();
//                }
//                break;
            case R.id.area_layout:
                updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? -1 : 1, null);
                mAddressPop.showPopwindow(binding.areaLayout);
                if (null == addressEntityList || addressEntityList.size() == 0) {
                    model.getCityData();
                }
                break;
            case R.id.sort_layout:
                if (null != sortData && sortData.size() > 0) {
                    updateChooseTextStatus(binding.sortLayout, binding.tvSort, "排序".equals(binding.tvSort.getText()) ? -1 : 1, null);
                    pickSortPopWindow.showPopwindow(binding.tvSort);
                }
                break;
            case R.id.state_layout:
                if (null != stateData && stateData.size() > 0) {
                    updateChooseTextStatus(binding.stateLayout, binding.tvState, "核算金额".equals(binding.tvState.getText()) ? -1 : 1, null);
                    pickMoneyPopWindow.showPopwindow(binding.tvState);
                }
                int value = getItemPos(PickCateType.TYPE_MONEY);
                pickMoneyPopWindow.setData(getItemData(PickCateType.TYPE_MONEY), value);
                break;
            case R.id.filter_layout://更多筛选
                mLeftDialog2.initData(mData, mFilters);
                mLeftDialog2.showDialog();
                break;
            default:
                break;
        }
    }

    private void showClassPop() {
        if ("2".equals(categoryType)) {//车辆求租
            onResetCarByClassData();
        } else {
            for (AskLeaseItemEntity<List<AskLeaseItemEntity>> l : allDatas) {
                PickSelectEntity entity = new PickSelectEntity();
                entity.setNo(l.getNo());
                entity.setProjectscale(l.getName());
                classDatas.add(entity);
            }
        }

        if ("5".equals(categoryType)) {
            binding.typeLayout.setVisibility(View.GONE);
        }

        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("");
        entity.setProjectscale("全部");
        classDatas.add(0, entity);
//        classPopWindow.setData(classDatas);
//        classPopWindow.goodsTypeAdapter.setDefItem(0);

        for (int i = 0; i < classDatas.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(classDatas.get(i).getProjectscale()));
        }
        binding.tabLayout.selectTab(binding.tabLayout.getTabAt(0));
    }

//    private void showTypePop() {
//        if ("类型".equals(binding.tvType.getText()) || "品牌".equals(binding.tvType.getText())) {
//            updateChooseTextStatus(binding.typeLayout, binding.tvType, -1, null);
//        } else {
//            updateChooseTextStatus(binding.typeLayout, binding.tvType, 1, null);
//        }
//        typePopWindow.showPopwindow(binding.typeLayout);
//        typePopWindow.setData(typeDatas);
//    }

    private void showTypeTab() {
        if (typeDatas == null || typeDatas.size() == 0) {
            binding.tabType.setVisibility(View.GONE);
            binding.viewTypeTabLine.setVisibility(View.GONE);
            return;
        }

        //设置类型tab
        if ("不限".equals(typeDatas.get(0).getProjectscale().trim())) {
            typeDatas.get(0).setProjectscale("全部");
            if (typeDatas.size() == 1) {
                binding.tabType.setVisibility(View.GONE);
                binding.viewTypeTabLine.setVisibility(View.GONE);
                return;
            } else {
                binding.tabType.setVisibility(View.VISIBLE);
                binding.viewTypeTabLine.setVisibility(View.VISIBLE);
            }
        }

        binding.tabType.removeAllTabs();
        for (int i = 0; i < typeDatas.size(); i++) {
            binding.tabType.addTab(binding.tabType.newTab().setText(typeDatas.get(i).getProjectscale()));
        }
        binding.tabType.selectTab(binding.tabType.getTabAt(0));
    }


    /**
     * 排序数据
     */
    public void setSortData(List<PickSelectEntity> datas) {
        this.sortData = datas;
        if (sortData != null && sortData.size() > 0) {
            pickSortPopWindow.setData(sortData, 0);
        }
    }

    /**
     * 概算金额
     *
     * @param datas
     */
    public void setPriceDatas(List<PickSelectEntity> datas) {
        this.stateData = datas;
        if (stateData != null && stateData.size() > 0) {
            pickMoneyPopWindow.setData(stateData, 0);
            addEntity(PickCateType.TYPE_MONEY, datas);
        }
    }

    public void setTimeDatas(List<PickSelectEntity> datas) {
        addEntity(PickCateType.PUBLISH_TIME, datas);
    }

    /**
     * 省市区数据信息
     *
     * @param data
     */
    public void showAddressDatas(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data, String provinceNo) {
        this.addressEntityList = data;
        if (data != null && data.size() > 0) {
            this.provinceNo = provinceNo;
            mAddressPop.setData(data, true, provinceNo);
        }
    }

    private void checkDismiss(int resId) {
        if (mAddressPop != null && mAddressPop.isShowing() && R.id.area_layout != resId) {
            mAddressPop.dismiss();
        }
    }


    //更新选项颜色
    private void updateChooseTextStatus(View view, TextView textView, int value, String name) {
        if (value == 0) {
            setTextViewBg(view, textView, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
        } else if (value == -1) {
            setTextViewBg(view, textView, R.drawable.search_bg, R.mipmap.ic_up_arrow_n, R.color.color_333333);
        } else if (value == 1) {
            setTextViewBg(view, textView, R.drawable.search_new_blue_bg, R.mipmap.icon_up_red, R.color.color_main);
        } else if (value == 2) {
            setTextViewBg(view, textView, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
        }
        if (TextUtils.isEmpty(name)) return;
        textView.setText(name);
    }

    //设置背景色和图标
    private void setTextViewBg(View view, TextView textView, int bg, int img, int textColor) {
        view.setBackgroundResource(bg);
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable_n, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    private SparseArray<PickSelectTextEntity> mData = new SparseArray<>();
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> nameArray = new SparseArray<>();

    private void initChooseDialog() {
        nameArray.put(PickCateType.PUBLISH_TIME, "发布时间");
        nameArray.put(PickCateType.TYPE_MONEY, "概算金额");
        mLeftDialog2 = new PickChooseLeftDialog2(getActivity(), nameArray);
        mLeftDialog2.setCallBack(new PickChooseLeftDialog2.DialogCallBackImpl() {
            @Override
            public void loadCategory(PickSelectTextEntity no, int firPos) {

            }

            @Override
            public void loadData(SparseArray<PickSelectTextEntity> data, SparseIntArray map) {
                mData = PickCopyUtils.deepCopy(data);
                mFilters = map.clone();

                for (int i = 0; i < mData.size(); i++) {
                    int key = mData.keyAt(i);
                    PickSelectTextEntity entity = mData.get(key);
                    mFilters.put(entity.getSort(), mFilters.get(entity.getSort()));
                    updateChooseStatus(entity.getSort(), mFilters.get(entity.getSort()));
                }

                initFilterData(false);
            }

            @Override
            public void reset() {
                mFilters.clear();
                loadData(mData, mFilters);
                mLeftDialog2.initData(mData, mFilters);
                mLeftDialog2.showDialog();
            }

            @Override
            public void onCancel() {
                mLeftDialog2.initData(mData, mFilters);
            }
        });
    }

    public void addEntity(int index, List<PickSelectEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
        } else {
            mData.put(index, new PickSelectTextEntity(index, nameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    /**
     * 筛选赋值
     */
    private void updateChooseStatus(int key, int value) {
        mFilters.put(key, value);
        String no = mData.get(key).getItemNo(value);
        String name = mData.get(key).getItemName(value);
        switch (key) {
            case PickCateType.TYPE_MONEY: // 概算金额
                price = no;
                updateChooseTextStatus(binding.stateLayout, binding.tvState, 2, name);
                break;
            case PickCateType.PUBLISH_TIME: // 发布时间
                timeNo = no;
                break;
        }
    }

    /**
     * 获取当前缓存位置下标
     *
     * @param pos index
     */
    public int getItemPos(int pos) {
        return mFilters.get(pos);
    }

    public List<PickSelectEntity> getItemData(int index) {
        if (mData.get(index) != null) {
            return mData.get(index).getPickSelectEntityList();
        }
        return new ArrayList<>();
    }

    //提供给外部调用
    public void onLoadMore() {
        initFilterData(true);
    }
}
